package org.ehe.finance.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.utils.MapstructUtils;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.finance.currency.domain.ErpFinanceCurrency;
import org.ehe.finance.currency.domain.bo.ErpFinanceCurrencyBo;
import org.ehe.finance.customer.domain.ErpFinanceCustomer;
import org.ehe.finance.customer.domain.bo.ErpFinanceCustomerBo;
import org.ehe.finance.customer.domain.vo.ErpFinanceCustomerVo;
import org.ehe.finance.customer.mapper.ErpFinanceCustomerMapper;
import org.ehe.finance.customer.service.ErpFinanceCustomerService;
import org.ehe.finance.voucher.service.ErpFinanceVoucherDetailService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 客户档案ServiceImpl
 * @author :zhangnn
 * @className :ErpFinanceCustomerServiceImpl
 * @description: TODO
 * @date 2025-08-27 16:38:12
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ErpFinanceCustomerServiceImpl extends ServiceImpl<ErpFinanceCustomerMapper, ErpFinanceCustomer>
    implements ErpFinanceCustomerService {

    private final ErpFinanceVoucherDetailService voucherDetailService;
    /**
     * 分页
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<ErpFinanceCustomerVo> queryPageList(ErpFinanceCustomerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpFinanceCustomer> lqw = buildQueryWrapper(bo);
        Page<ErpFinanceCustomerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
    /**
     * 查询客户信息列表
     */
    @Override
    public List<ErpFinanceCustomerVo> queryList(ErpFinanceCustomerBo bo) {
        LambdaQueryWrapper<ErpFinanceCustomer> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpFinanceCustomer> buildQueryWrapper(ErpFinanceCustomerBo bo) {
        LambdaQueryWrapper<ErpFinanceCustomer> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCustomerName()), ErpFinanceCustomer::getCustomerName, bo.getCustomerName());
        lqw.eq(StringUtils.isNotBlank(bo.getContactPerson()), ErpFinanceCustomer::getContactPerson, bo.getContactPerson());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), ErpFinanceCustomer::getPhone, bo.getPhone());
        lqw.eq(bo.getStatus() != null, ErpFinanceCustomer::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 查询客户信息
     */
    @Override
    public ErpFinanceCustomerVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 新增客户信息
     */
    @Override
    public Boolean insertByBo(ErpFinanceCustomerBo bo) {
        ErpFinanceCustomer add = MapstructUtils.convert(bo, ErpFinanceCustomer.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户信息
     */
    @Override
    public Boolean updateByBo(ErpFinanceCustomerBo bo) {
        ErpFinanceCustomer update = MapstructUtils.convert(bo, ErpFinanceCustomer.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpFinanceCustomer entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除客户信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 修改用户状态
     *
     * @param id 客户ID
     * @param status 帐号状态
     * @return 结果
     */
    @Override
    public int updateCustomerStatus(Long id, Integer status) {
        return baseMapper.update(null,
            new LambdaUpdateWrapper<ErpFinanceCustomer>()
                .set(ErpFinanceCustomer::getStatus, status)
                .eq(ErpFinanceCustomer::getId, id));
    }
}
